package com.dyh.quickdemozwei.deliver;

import java.util.HashMap;
import java.util.Map;

public class NumberTable extends BaseTable<Integer> {
    public Map<String,Integer> sumBy(int idx){
        Map<String,Integer> res = new HashMap<>();

        for (Map.Entry<String, Row<Integer>> entry : this.datas.entrySet()) {
            Row<Integer> row = entry.getValue();
            String nk = row.getTags()[idx];

            res.putIfAbsent(nk,0);
            res.computeIfPresent(nk,(k,v)->v+row.getVal());
        }


        return res;
    }


    public void sumData(int num,String[] tags){

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < tags.length; i++) {
            tags[i]=tags[i].toString();
            sb.append(tags[i]).append("#");
        }
        String key = sb.substring(0,sb.length()-1);

        this.datas.putIfAbsent(key,new Row<>(tags,0));
        this.datas.computeIfPresent(key,(k,v)->{
            v.setVal(v.getVal()+num);
            return v;
        });

    }

    public void sumData(int num,Object...args){
        String[] tags = new String[args.length];
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < args.length; i++) {
            tags[i]=args[i].toString();
            sb.append(args[i]).append("#");
        }
        String key = sb.substring(0,sb.length()-1);

        this.datas.putIfAbsent(key,new Row<>(tags,0));
        this.datas.computeIfPresent(key,(k,v)->{
            v.setVal(v.getVal()+num);
            return v;
        });

    }


}
